<?php
/**
 * 今天值得买用户中心
 * @author wubaiqing<wubaiqing@vip.qq.com>
 * @copyright Copyright (c) 2013 今天值得买
 * @since 1.5
 */
class UserController extends Controller
{
    /**
     * 页面访问权限
     * @return array 权限列表
     */
    public function accessRules()
    {
        return array();
    }

    /**
     * 用户注册
     */
    public function actionRegister()
    {
        $model = new User;
        $model->scenario = 'register';
        if (isset($_POST['User'])) {
            $model->attributes = $_POST['User'];
            if ($model->save()) {
                echo '123';
            }
        }
        $this->render('register', array(
            'model' => $model
        ));
    }

    /**
     * 密码管理
     */
    public function actionPasswordManager()
    {
        $userId = Yii::app()->user->id;
        $userId = '1';

        $model = User::model()->findByPk($userId);
        $model->scenario = 'passwordManager';
        if (isset($_POST)) {
            $post = $_POST['User'];
            $model->attributes = $post;
            if ($model->save()) {
                echo '123';
            } else {
                var_dump($model->getErrors());

            }
        }
        //$this->render('password-manager');
    }

    /**
     * 用户签到
     */
    public function actionUserSign()
    {
        $userId = Yii::app()->user->id;
        $userId = '1';
        User::userSign($userId);
    }

    /**
     * 积分管理
     */
    public function actionScoreManage()
    {

    }

    /**
     * 导出csv
     */
    public function actionGetGold()
    {
        if (isset($_POST['Gold'])) {
            $startTime = !empty($_POST['Gold']['start_time']) ? strtotime($_POST['Gold']['start_time']) : strtotime('today 00:00:00') ;
            $endTime = $startTime + 86400;
            $gold = Gold::model()->findAll(array(
                'condition' => 'status =:status And created_at >:start_time And created_at <:end_time',
                'params' => array(':status' => 0, ':start_time' => $startTime, ':end_time' => $endTime)
            ));
            $array = array();
            $csv = "收款帐号,发放集分宝数（个）,QQ号\n";
            foreach ($gold as $key => $val) {
                $csv.= "{$val->alipay},30,{$val->qq}\n";
            }
            header("Content-Type:text/csv");
            header("Content-Disposition:attachment; filename=集分宝导入.csv");
            header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
            header("Expires:0");
            header("Pragma:public");
            echo $csv;
            Yii::app()->end();
        }
        $this->render('gold');
    }
}
